home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Languguage OS 2
/
Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO
/
language
/
embedded
/
simulato
/
v2_3_mc6.tz
/
v2_3_mc6
/
testfiles
/
bin2hex.asm
next >
Wrap
Assembly Source File
|
1994-05-02
|
5KB
|
120 lines
**** SUBROUTINE BIN2HEX ****
* This purpose of this code is to convert a string whose representation
* is of a binary input and convert it to a string whose representation
* is of a hex input. The user is required to pass the parameters on the
* stack by standard convention as follows:
*
* |---------------------------------------------------|
* | POINTER TO STRING TO BE CONVERTED(LONGWORD) |
* |---------------------------------------------------|
* | POINTER TO PLACE WHERE OUTPUT STRING IS(LONGWORD) |
* |---------------------------------------------------|
* | NEGATIVE FLAG(WORD) |
* |---------------------------------------------------|
*
* The negative flag in this routine is an output, and may be retreived
* by the user for their program's. However, the returned string will always
* be converted to a 2's complement positive representation.
*****************************
**** RESOURCES ****
*
* a2 -> pointer to first char. in string to be converted.
* a5 -> pointer to output location.
* d2 -> read char/overall bit count.
* d3 -> holder of the negative flag.
* d4 -> temporay convereted result.
* d5 -> counter for temp. result.
* d6 -> result overall.
* d7 -> scratch reg.
*
*******************
SECTION B2H,code ;define section for this
XDEF BIN2HEX ;sub. and tell of exeternal use.
BIN2HEX LINK A6,#0 ;take care of standard param. pass.
MOVEM.L A0-A5/D0-D2/D4-D7,-(A7) ;save all reg. but d3 because output.
CLR.L D3 ;clear neg. flag.
CLR.L D4 ;clear temp converted digit.
CLR.L D5 ;clear counter for digit.
CLR.L D6 ;clear overall result.
MOVEA.L 8(A6),A2 ;move in param. for parsed string.
MOVEA.L 12(A6),A5 ;move in param. for output string.
B_STR2NUM MOVE.B (A2)+,D2 ;move in most sig. char from input.
CMPI.B #0,D2 ;if found char. is NULLL then
BEQ.W MAKE_HEX_STR ;branch to place in string
CMPI.B #' ',D2 ;else if found char. is SPACE
BEQ.S B_STR2NUM ;ignore.
ROXR.B #1,D2 ;else place bit 0 in extend.
ROXL.W #1,D6 ;put found bit in temp result.
BRA.S B_STR2NUM ;repeat till the end of string.
MAKE_HEX_STR TST.W D6 ;if the overall result is
BMI.S B_NEG ;negative take care of it.
BNE.S B_FIND1 ;else check for zero if not ok
MOVE.B #'0',(A5)+ ;else put zero in string
BRA.W END_B2H ;and return.
B_NEG ADDQ.W #1,D3 ;set negative flag.
MOVE.W D3,16(A6) ;put return param. on stack
NEG.W D6 ;negate the results.
B_FIND1 CLR.L D2 ;clear overall counter.
1$ ROXL.W #1,D6 ;move MSB of result to extend
ROXL.W #1,D4 ;move that bit to 4 bit result
ADDQ.W #1,D5 ;increment the local bit counter.
ADDQ.W #1,D2 ;increment total bit count.
CMPI.W #4,D5 ;test if this is the end of nib.
BNE.S 1$ ;if not cont w/ rotation.
TST.W D4 ;is 4 bit result 0
BNE.S B_PUT1 ;if not place 1st char in string.
CLR.W D4 ;clear 4 bit result.
CLR.W D5 ;clear 4 bit counter.
BRA.S 1$ ;repeat process.
B_PUT1 CMPI.W #9,D4 ;see if 4 bit result is > 9
BLE.S B_DIGISNUM ;if so then it is chr.
B_DIGISCHR ADDQ.W #7,D4 ;so add chr. offset.
B_DIGISNUM ADDI.W #$30,D4 ;add ASCII offset.
MOVE.B D4,(A5)+ ;move into output string.
CLR.W D4 ;clear 4 bit result.
CLR.W D5 ;clear 4 bit counter.
CMPI.W #16,D2 ;is total bit count @ end.
BEQ.S END_B2H ;if so end.
B_FIND2END ROXL.W #1,D6 ;else move MSB of result to extend
ROXL.W #1,D4 ;move bit 4 bit holder.
ADDQ.W #1,D2 ;inc. overall bit count.
ADDQ.W #1,D5 ;inc. 4 bit count.
CMPI.W #4,D5 ;test for end of nibble.
BNE.S B_FIND2END ;if not repeat
BRA.S B_PUT1 ;else place in string.
END_B2H MOVE.B #0,(A5) ;put terminator in string.
MOVEM.L (A7)+,D0-D2/D4-D7/A0-A5 ;restore all registers.
UNLK A6 ;restore stack to entry point.
RTS ;return
END